package com.xiaodu.acer.phoneplay.ui.fragment;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.xiaodu.acer.phoneplay.R;
import com.xiaodu.acer.phoneplay.bean.IndexBean;
import com.xiaodu.acer.phoneplay.di.component.AppComponent;
import com.xiaodu.acer.phoneplay.di.component.DaggerRecommendComponent;
import com.xiaodu.acer.phoneplay.di.module.RecommendModule;
import com.xiaodu.acer.phoneplay.presenter.RecommendPresenter;
import com.xiaodu.acer.phoneplay.presenter.contract.AppInfoContract;
import com.xiaodu.acer.phoneplay.ui.adapter.RecomendAdatper;
import com.xiaodu.acer.phoneplay.ui.base.ProgressFragment;
import com.xiaodu.acer.phoneplay.ui.decoration.DividerItemDecoration;

import javax.inject.Inject;

import butterknife.BindView;
import zlc.season.rxdownload2.RxDownload;

/**
 * 推荐Fragment
 */
public class RecommendFragment extends ProgressFragment<RecommendPresenter>
        implements AppInfoContract.View {

    @BindView(R.id.recycle_view)
    RecyclerView mRecyclerView;

    private RecomendAdatper mAdatper;

    @Inject
    RxDownload mRxDownload;



    @Override
    public void init() {
        initRecycleView();
        mPresenter.requestDatas();
    }

    @Override
    public int setLayout() {
        return R.layout.fragment_recomend;
    }

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {
        DaggerRecommendComponent.builder()
                .appComponent(appComponent)
                .recommendModule(new RecommendModule(this))
                .build()
                .inject(this);
    }


    private void initRecycleView() {
        //为RecyclerView设置布局管理器
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        //为RecyclerView设置分割线(这个可以对DividerItemDecoration进行修改，自定义)
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.HORIZONTAL_LIST));
        //动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    }


    @Override
    public void showResult(IndexBean indexBean) {
        mAdatper = new RecomendAdatper(getActivity(), mRxDownload);
        mAdatper.setDatas(indexBean);
        mRecyclerView.setAdapter(mAdatper);
    }

    @Override
    public void onRequestPermissonSuccess() {
        mPresenter.requestDatas();
    }

    @Override
    public void onRequestPermissonError() {
        Toast.makeText(getActivity(), "你已拒绝授权", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onEmptyViewClick() {
        mPresenter.requestDatas();
    }
}
